<?php
class SmsTempletAction extends BaseAction {
	
	public function index() {
		$this->display();
	}
	

	public function doDelete() {
		$imgpath	= $_POST['imgpath'];
		if (empty($imgpath)) {
			echo 0;
			return ;
		}
		$map['imgpath']=$imgpath;
		$where['is_delete']='Y';
		$result =  M('SmsTemplet')->where($map)->save($where); 
		echo $result ? '1' : '0';
	}
	

	/**
	*	获取彩信图片模板
	**/
	public function getJsonSmsTemplet(){
		$sort_id = $_REQUEST['sort_id'];
		if(!empty($sort_id)&&$sort_id!='all')$where['sort_id']=$sort_id;
		$where['is_delete']='N';
		$where['is_uploadbyuser']='N';
		$limit = 12;
		$order = 'ctime desc';
		$smstdao = M('SmsTemplet');
			
			/**
			if($sort_id=='my'){
				unset($where['sort_id']);
				$where['is_uploadbyuser']='Y';
				$where['insert_user_id']=$this->mid;
			}
			**/
			
		// 查询总数
		$count_options['where']	=	$where;
		$count_options['limit'] = 1;
		$count_options['field'] = 'count(1) as count';
		// 去掉统计时的排序提高效率
		$result =	$smstdao->where($where)->limit(1)->field('count(1) as count')->find();
		//dump($result);
		$count	=	$result['count'];
		//echo 'count='.$count;
		//如果总数大于零，则进行进一步数据查询
		if($count > 0) {
			$p	=	new KPage($count,$limit,2);
			// 查询数据
			$shelflist = $smstdao->where($where)->order($order)
													->limit($p->firstRow.','.$p->listRows)->findAll();
			//dump($vShelfPage);
			// 输出控制
			$output['count']		=	$count;
			$output['totalPages']	=	$p->totalPages;
			$output['totalRows']	=	$p->totalRows;
			$output['nowPage']		=	$p->nowPage;
			$output['html']			=	$p->show();
			$output['data']			=	$shelflist;
			unset($p);
			unset($count);
		}else{
			$output['count']		=	0;
			$output['totalPages']	=	0;
			$output['totalRows']	=	0;
			$output['nowPage']		=	1;
			$output['html']			=	'';
			$output['data']			=	'';
		}
		//dump($output);
		exit(json_encode($output)); 
	}
	
	//彩信图片上传处理
  	function smsimg(){
   		$type = $_REQUEST['t'];
  		$pSmsImg = service('SmsImg');
   		$pSmsImg ->isAdmin = true;
    	if( $type == 'upload' ){
       		echo $pSmsImg->upload();
   		}elseif ( $type == 'save'){
   			$sort_id = $_REQUEST['sort_id'];
   			$title = $_REQUEST['title'];
   			if(empty($title)){
   				$result['msg']='请输入名称！';
				$result['status']=0;
				exit( json_encode( $result ) );
   			}
   			if(empty($sort_id)){
   				$result['msg']='请选择类型！';
				$result['status']=0;
				exit( json_encode( $result ) );
   			}
   			$result['msg']='新增失败！';
   			$result['smsimg_path']='';
			$result['status']=0;
       		$smsimg_relpath = $pSmsImg->dosave();
       		/** 如果生成成功，则保存相关数据 **/
       		if(!empty($smsimg_relpath)){
       			$map['title'] = $title;
				$map['sort_id'] = $sort_id;
				$map['ctime'] 	=	time();
				$map['is_uploadbyuser'] 	=	'N';
				$map['is_delete'] 	=	'N';
				//$map['insert_user_id'] 	=	$this->mid;
				$map['imgpath'] = $smsimg_relpath;
				$id = M('SmsTemplet')->add($map);
				if($id){
					$result['msg']='新增成功！';
		   			$result['smsimg_path']=$smsimg_relpath;
					$result['status']=1;
				}
       		}
			echo json_encode( $result );
		}
  		exit;     		
  	}
	
	/**
	*	预览彩信图片
	**/
	public function preview() {
     	$smsimg = $_REQUEST['smsimg'];
     	$smstext = $_REQUEST['smstext'];
     	$xyOffset = $_REQUEST['xyOffset'];
     	$textcolor = $_REQUEST['textcolor'];
     	$ispreview = $_REQUEST['ispreview'];//是否生成后的保存图片
     	$smsicon = $_REQUEST['smsicon'];//表情图片路径

     	$smsImgDao = service('SmsImg');
     	$smsImgDao ->isAdmin = true;
     	
     	$imgdata_ = $smsImgDao->parseRequestData($xyOffset,$textcolor,$font_file,$font_size);
     	$im = $smsImgDao->writeTxtImg2Bg(SITE_PATH.getAttachUrl($smsimg),SITE_PATH.$smsicon
     															,$smstext,$imgdata_['xyOffset'],$imgdata_['rgb']
																,$imgdata_['font_file'],$imgdata_['font_size']);
     	if($ispreview!='yes'){
			$tmp_smsimgPath = $smsImgDao->doSaveUserTmpTemplet($im,$smsimg);
			ImageDestroy($im);
			$result['tmp_smsimgPath']=$tmp_smsimgPath;
			$result['status']=1;
			echo json_encode( $result );
			exit;
		}
		//echo 'kim';
		// output image  
		Header ('Content-type: image/png');//即便是从jpg拷贝的图片，也能以png输出，  
		ImagePng ($im);  
		// clean up  
		ImageDestroy($im);   
		exit;
	}
	
}
?>